package com.thwj.project.app.service;

import com.thwj.project.biz.domain.User;
import com.thwj.project.biz.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * Copyright (C), 
 *
 * @author: 可乐
 * @date: 2019/06/04 14:01
 * @description:
 */
@Component
public class UserInfoResolver {


    @Autowired
    private IUserService userService;


    /**
     * @param targetList 需要补全的对象列表
     * @param getUserId  每个对象获取用户id的方法
     * @param resolver   对每个对象赋值的回调方法
     * @param <T>
     */
    public <T> void resolve(List<T> targetList, Function<T, Long> getUserId, InfoResolve<T> resolver) {
        List<Long> userIdList = targetList.stream().map(getUserId).filter(i -> null != i).distinct().collect(Collectors.toList());
        if(CollectionUtils.isEmpty(userIdList)) {
            return;
        }
        List<User> userList = userService.listByIds(userIdList);
        targetList.forEach(i -> {
            List<User> userListOne = userList.stream().filter(DO->DO.getId().compareTo(getUserId.apply(i)) == 0).collect(Collectors.toList());
            resolver.resolve(i, userListOne.get(0).getUserNickName(),userListOne.get(0).getUserHeadPic(),userListOne.get(0).getGender(),userListOne.get(0).getAuthenticationType());
        });
    }

    public <T> void resolve(List<T> targetList, Function<T, Long> getUserId, InfoResolveContact<T> resolver) {
        List<Long> userIdList = targetList.stream().map(getUserId).filter(i -> null != i).distinct().collect(Collectors.toList());
        if(CollectionUtils.isEmpty(userIdList)) {
            return;
        }
        List<User> userList = userService.listByIds(userIdList);
        targetList.forEach(i -> {
            List<User> userListOne = userList.stream().filter(DO->DO.getId().compareTo(getUserId.apply(i)) == 0).collect(Collectors.toList());
            resolver.resolve(i, userListOne.get(0).getUserNickName(),userListOne.get(0).getUserHeadPic(),userListOne.get(0).getGender(),userListOne.get(0).getSignature(), userListOne.get(0).getPhone(),userListOne.get(0).getWechat());
        });
    }

    public <T> void resolve(List<T> targetList, Function<T, Long> getUserId, InfoResolveOfficer<T> resolver) {
        List<Long> userIdList = targetList.stream().map(getUserId).filter(i -> null != i).distinct().collect(Collectors.toList());
        if(CollectionUtils.isEmpty(userIdList)) {
            return;
        }
        List<User> userList = userService.listByIds(userIdList);
        targetList.forEach(i -> {
            List<User> userListOne = userList.stream().filter(DO->DO.getId().compareTo(getUserId.apply(i)) == 0).collect(Collectors.toList());
            resolver.resolve(i, userListOne.get(0).getUserNickName(),userListOne.get(0).getUserHeadPic(),userListOne.get(0).getOfficeUser());
        });
    }

    public <T> void resolve(T target, Function<T, Long> getUserId, InfoResolve<T> resolver) {
        User user = userService.getById(getUserId.apply(target));

        resolver.resolve(target, user.getUserNickName(),user.getUserHeadPic(),user.getGender(),user.getAuthenticationType());

    }

    /**
     * @param targetList 需要补全的对象列表
     * @param getUserId  每个对象获取用户id的方法
     * @param resolver   对每个对象赋值的回调方法
     * @param <T>
     */
    public <T> void resolve(List<T> targetList, Function<T, Long> getUserId, InfoOnlyResolve<T> resolver) {
        List<Long> userIdList = targetList.stream().map(getUserId).filter(i -> null != i).distinct().collect(Collectors.toList());
        if(CollectionUtils.isEmpty(userIdList)) {
            return;
        }
        List<User> userList = userService.listByIds(userIdList);
        targetList.forEach(i -> {
            if(getUserId.apply(i) != null) {
                List<User> userListOne = userList.stream().filter(DO->DO.getId().compareTo(getUserId.apply(i)) == 0).collect(Collectors.toList());
                if (!CollectionUtils.isEmpty(userListOne)) {
                    resolver.resolve(i, userListOne.get(0).getUserNickName(),userListOne.get(0).getUserHeadPic());
                }

            }
        });
    }
    /**
     * @param target 需要补全的对象列表
     * @param getUserId  每个对象获取用户id的方法
     * @param resolver   对每个对象赋值的回调方法
     * @param <T>
     */
    public <T> void resolve(T target, Function<T, Long> getUserId, InfoOnlyResolve<T> resolver) {
        User user = userService.getById(getUserId.apply(target));
        if(user == null) {
            return;
        }
        resolver.resolve( target, user.getUserNickName(), user.getUserHeadPic());
    }

    public interface InfoResolve<T> {
        void resolve(T obj, String nickName, String headPic, Integer gender,Integer authenticationType);
    }
    public interface InfoOnlyResolve<T> {
        void resolve(T obj, String nickName, String headPic);
    }

    public interface InfoResolveContact<T> {
        void resolve(T obj, String nickName, String headPic, Integer gender,String signature,String phone,String wechat);
    }
    public interface InfoResolveOfficer<T> {
        void resolve(T obj, String nickName, String headPic, Boolean officer);
    }

}
